cd "C:\DRS\"

use VAR_LARGE_BENCHM.dta, clear
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using AT8.log, replace
** Simulations Minimizing the Variance of Returns **
** Largest No. of Funds **
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close

use VAR_LONG_BENCHM.dta, clear
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using AT8.log, append
** Simulations Minimizing the Variance of Returns **
** Longest Available Sample **
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close

use EXP_LARGE_BENCHM.dta, clear
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using AT8.log, append
** Simulations Maximizing Expected Returns **
** Largest No. of Funds **
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close

use EXP_LONG_BENCHM.dta, clear
sort simid t
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using AT8.log, append
** Simulations Maximizing Expected Returns **
** Longest Available Sample **
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close
